//
//  FTTextFieldCell.m
//  WebPDA
//
//  Created by Justin Yip on 8/15/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTTextFieldCell.h"


@implementation FTTextFieldCell
@synthesize textField;

-(void)editingDidEndOnExit:(id)sender {
	NSString *key = [preferenceSpecifier objectForKey:@"Key"] != nil ? [preferenceSpecifier objectForKey:@"Key"] : [preferenceSpecifier objectForKey:@"Title"];
	[cellValueObserver changeValue:textField.text forKey:key];
	[self resignFirstResponder];
}

-(void)configureWithEntity:(id)entity preferenceSpecifier:(NSDictionary*)ps {
	
	[super configureWithEntity:entity preferenceSpecifier:ps];
	
	[self.textField addTarget:self action:@selector(editingDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
	
	/*
	 mark as secure
	 */
	if((CFBooleanRef)[ps objectForKey:@"IsSecure"] == kCFBooleanTrue){
		
		self.textField.secureTextEntry = YES;
	}else {
		
		self.textField.text = [ps objectForKey:@"DefaultValue"];
	}
	
	/*
	 set value from entity
	 */
	//getter selector
	NSString *key = [preferenceSpecifier objectForKey:@"Key"] != nil ? [preferenceSpecifier objectForKey:@"Key"] : [preferenceSpecifier objectForKey:@"Title"];
	if (key) {
	SEL selector = NSSelectorFromString(key);
	if (nil != entity && [entity respondsToSelector:selector]) {
		id value = [entity performSelector:selector];
		textField.text = value;
	}
	}
	
	self.textField.keyboardType = UIKeyboardTypeAlphabet;
	if([ps objectForKey:@"KeyboardType"] != nil){
		
		if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"Alphabet"]){
			
			self.textField.keyboardType = UIKeyboardTypeAlphabet;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"NumberPad"]){
			
			self.textField.keyboardType = UIKeyboardTypeNumberPad;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"NumbersAndPunctuation"]){
			
			self.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"URL"]){
			
			self.textField.keyboardType = UIKeyboardTypeURL;
		}
		else if([[ps objectForKey:@"KeyboardType"] isEqualToString:@"EmailAddress"]){
			
			self.textField.keyboardType = UIKeyboardTypeEmailAddress;
		}
		
	}
	
	self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
	
	if([ps objectForKey:@"AutocapitalizationType"] != nil){
		
		if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"None"]){
			
			self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"Sentences"]){
			
			self.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"Words"]){
			
			self.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
		}
		else if([[ps objectForKey:@"AutocapitalizationType"] isEqualToString:@"AllCharacters"]){
			
			self.textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
		}
	}
	
	self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
	
	if([ps objectForKey:@"AutocorrectionType"] != nil){
		
		if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"Default"]){
			
			self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
		}
		else if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"No"]){
			
			self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
		}
		else if([[ps objectForKey:@"AutocorrectionType"] isEqualToString:@"Yes"]){
			
			self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
		}
		
	}
}

-(id)initWithFormBundle:(NSBundle*)bundle valueObserver:(id)observer reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithFormBundle:bundle valueObserver:observer reuseIdentifier:reuseIdentifier])) {
        // Initialization code
		textField = [[UITextField alloc] initWithFrame:CGRectMake(125,12,150,25)];
		textField.returnKeyType = UIReturnKeyDone;
		textField.borderStyle = UITextBorderStyleNone;
		[self.contentView addSubview:textField];
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
	[textField release];
    [super dealloc];
}


@end
